<?php

namespace Summer\Hjd\Http\Controllers;

use Illuminate\Routing\Controller;
use October\Rain\Support\Facades\Input;
use RLuders\JWTAuth\Classes\JWTAuth;
use Summer\Hjd\Models\RgRuleModel;
use Summer\Hjd\Models\WalletModel;
use Summer\Hub\Http\Controllers\Traits\ApiReturn;
use Summer\Hjd\Models\Settings as HjdSettings;
use Config;

class RgRuleController extends Controller
{
    use ApiReturn;

    /**
     * 充值规则
     */
    public function index(JWTAuth $auth)
    {
        if (!$user = $auth->user()) {
            return $this->apiError('登录失效,请重新登录');
        }
        $hjd_settings= HjdSettings::instance();
        //充值配置
        $rg_settings['web_phone']=$hjd_settings->web_phone;
        $rg_settings['wechat_number']=$hjd_settings->wechat_number;
        $rg_settings['alipay_number']=$hjd_settings->alipay_number;
        $rg_settings['bank_username']=$hjd_settings->bank_username;
        $rg_settings['bank_name']=$hjd_settings->bank_name;
        $rg_settings['bank_number']=$hjd_settings->bank_number;
        $rg_settings['bank_address']=$hjd_settings->bank_address;
        if($hjd_settings->wechat_cover){
            $rg_settings['wechat_cover']=$hjd_settings->wechat_cover->path;
        }
        if($hjd_settings->alipay_cover){
            $rg_settings['alipay_cover']=$hjd_settings->alipay_cover->path;
        }
        $return['rg_settings']=$rg_settings;
        $query=RgRuleModel::select();
        $query->with(['currency'=>function($q){
            $q->select('slug','title');
        }]);
        $query->whereHas('currency',function ($q){
            $q->where('status',1);
        });
        //可传参数，如果传则records[0]是该币种的配置
        if(Input::has('currency_slug')){
            $currency_slug=Input::get('currency_slug');
            $query->where('currency_slug',$currency_slug);
        }
        $query->where('status',1);
        $records=$query->get();
        $records->each(function ($record)use($user){
            $record->balance=WalletModel::where('currency_slug',$record->currency_slug)->where('user_id',$user->id)->value('balance');
        });
        $return['records']=$records;
        //快捷充值
//        $moneys[]=['money'=>'100'];
//        $moneys[]=['money'=>'200'];
//        $moneys[]=['money'=>'500'];
//        $moneys[]=['money'=>'1000'];
        $return['moneys']=$hjd_settings->quick_money;
        //收款类型
        $pay_types=[];
        $pay_type_config=Config::get('summer.hjd::rg_pay_type');
        foreach ($pay_type_config as $ptk=>$ptv){
            $arr['slug']=$ptk;
            $arr['title']=$ptv;
            $pay_types[]=$arr;
        }
        $return['pay_types']=$pay_types;
        return $this->apiSuccess('操作成功',$return);
    }
}
